package com.lanou.sinasports.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Environment;
import android.util.Log;

import com.android.volley.toolbox.ImageLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by dllo on 16/1/18.
 */
public class DiskCach implements ImageLoader.ImageCache {
    private String cacheDir="";

    public DiskCach() {
        if(Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED)){

            File root=Environment.getExternalStorageDirectory();
            File cacheFile=new File(root.getAbsolutePath()+"/cache");
            if(!cacheFile.exists()){
                cacheFile.mkdir();
            }
            cacheDir=cacheFile.getAbsolutePath();
        }
    }

    @Override
    public Bitmap getBitmap(String url) {
        url=url.substring(url.lastIndexOf("/"),url.lastIndexOf(".")+4);
//        return oldRemeber(BitmapFactory.decodeFile(cacheDir + "/" + url));
        return BitmapFactory.decodeFile(cacheDir+"/"+url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {

        url=url.substring(url.lastIndexOf("/"),url.lastIndexOf(".")+4);
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(cacheDir+"/"+url);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null!=fos){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


     public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
         final int height=options.outHeight;
         final int width=options.outWidth;
         int inSampleSize=1;
         if(height > reqHeight || width > reqWidth){
             final int halfHeight=height/2;
             final int halfWidth=width/2;

             while((halfHeight / inSampleSize) >reqHeight
                     && (halfWidth /inSampleSize) >reqWidth){
                 inSampleSize*=2;
             }
         }

         return inSampleSize;
     }

    public static Bitmap decodeSampledBitmapFromResource(String url,int resId,int reqWidth,int reqHeight){
        final BitmapFactory.Options options=new BitmapFactory.Options();

        options.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(url,options);
        options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);
        options.inJustDecodeBounds=false;

        return BitmapFactory.decodeFile(url,options);

    }

    //图片怀旧
    public static Bitmap oldRemeber(Bitmap bmp)
    {
        // 速度测试
        long start = System.currentTimeMillis();
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        int pixColor = 0;
        int pixR = 0;
        int pixG = 0;
        int pixB = 0;
        int newR = 0;
        int newG = 0;
        int newB = 0;
        int[] pixels = new int[width * height];
        bmp.getPixels(pixels, 0, width, 0, 0, width, height);
        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                pixColor = pixels[width * i + k];
                pixR = Color.red(pixColor);
                pixG = Color.green(pixColor);
                pixB = Color.blue(pixColor);
                newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);
                newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);
                newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);
                int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);
                pixels[width * i + k] = newColor;
            }
        }

        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        long end = System.currentTimeMillis();
        Log.d("may", "used time=" + (end - start));
        return bitmap;
    }
}
